package Main;

import kechengsheji.admin.Adminpanel;
import kechengsheji.employee.Userpanel;
import kechengsheji.manag.Managpanel;
import kechengsheji.admin.AdminDAO;
import kechengsheji.employee.UserDAO;
import kechengsheji.manag.ManagDAO;
import gongju.User;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Login extends JPanel {
    public Login() {
        this.setBounds(0, 0, 900, 600);
        this.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(200, 100, 500, 300);
        this.add(panel);

        Box box = Box.createVerticalBox();
        panel.add(box);
        box.add(Box.createVerticalStrut(15));

        Box box0 = Box.createHorizontalBox();
        JLabel title = new JLabel("登录页面");
        title.setFont(new Font("微软雅黑", Font.BOLD, 30));
        box0.add(title);
        box.add(box0);
        box.add(Box.createVerticalStrut(20));

        Font font = new Font("微软雅黑", Font.BOLD, 20);
        Border border = BorderFactory.createLoweredBevelBorder();

        Box box1 = Box.createHorizontalBox();
        JLabel lbluser = new JLabel("用户名:  ");
        lbluser.setFont(font);
        box1.add(lbluser);
        JTextField txtuser = new JTextField(15);
        txtuser.setBorder(border);
        box1.add(txtuser);
        box.add(box1);

        box.add(Box.createVerticalStrut(15));
        Box box2 = Box.createHorizontalBox();
        JLabel lblpwd = new JLabel("密   码:  ");
        lblpwd.setFont(font);
        box2.add(lblpwd);
        JPasswordField txtpwd = new JPasswordField(15);
        txtpwd.setBorder(border);
        box2.add(txtpwd);
        box.add(box2);

        box.add(Box.createVerticalStrut(15));

        JRadioButton AdminService = new JRadioButton("管理员", true);
        AdminService.setFont(font);
        AdminService.setOpaque(false);
        AdminService.setFocusPainted(false);

        ButtonGroup bg = new ButtonGroup();
        bg.add(AdminService);
        Box box3 = Box.createHorizontalBox();
        box3.add(AdminService);
        box.add(box3);
        box.add(Box.createVerticalStrut(15));

        JButton login = new JButton("登录");
        box.add(login);

        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String username = txtuser.getText();
                String password = new String(txtpwd.getPassword());

                User user = new User();
                JPanel panel = null;
                if(AdminService.isSelected()){
                    user = AdminDAO.getUser(username);
                    panel = new Adminpanel();
                }else {
                    user = UserDAO.getUser(username);
                    panel = new Userpanel();
                }
                if(user == null || !user.getPassword().equals(password)){
                    JOptionPane.showMessageDialog(login.getParent(),"用户名或密码错误","系统提示",
                            JOptionPane.WARNING_MESSAGE);
                }else{
                    MainJFrame.setContent(panel);
                    MainJFrame.user=user;
                }
            }
        });
    }
}
